<?php

namespace App\Http\Controllers;

use App\MyImage;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Redirect;
use PhpSpec\Exception\Exception;
use Illuminate\Support\Facades;

class TestController extends Controller
{

    public function showImages()
    {
        $myimages=MyImage::all();
        $imgids=array();
        foreach($myimages as $myimage)
        {
            $imgids[]=$myimage->id;
        }
        return view('dispImage',['imgids'=>$imgids]);


    }
    public function getImageData($id)
    {
        $myimage=MyImage::findOrFail($id);
        $response = response($myimage->imgdata, 200);
        $response->header('Content-Type', 'image/jpeg');
        return $response;

    }
    public function postImage(Request $request)
    {
        $this->validate($request,[
            'imgtxt'=>'required|string|max:255',
            'imgfile'=>'required|image|max:16000',
        ]);


        try {
            if(!$request->has('imgtxt'))
            {
                $errorinfo = '必须输入图片描述文字';
                throw new Exception($errorinfo);
            }
            /*else
                $request->flashonly('imgtxt');*/
            if ($request->hasFile('imgfile')) {
                if ($request->file('imgfile')->isValid()) {
                    $file=$request->file('imgfile');
                    $tmppath=$file->getRealPath();
                    $tmprealfilename=$tmppath;
                    $imgtype=exif_imagetype($tmprealfilename);
                    if($imgtype==IMAGETYPE_GIF||$imgtype==IMAGETYPE_JPEG||$imgtype==IMAGETYPE_PNG||$imgtype==IMAGETYPE_BMP)
                    {

                        /*$errorinfo = '图片格式正确，一切都很好';
                        throw new Exception($errorinfo);*/
                        $myimage=new MyImage;
                        $myimage->imgtxt=$request->input('imgtxt');
                        //$imgdata =addslashes(file_get_contents($request->file('imgfile')->getRealPath()));
                        $imgdata =file_get_contents($request->file('imgfile')->getRealPath());
                        $myimage->imgdata=$imgdata;
                        $myimage->save();
                        return redirect('dispimg');

                    }
                    else
                    {
                        $errorinfo = '不支持的图片格式';
                        throw new Exception($errorinfo);
                    }


                } else {
                    $errorinfo = '上传文件无效';
                    throw new Exception($errorinfo);
                }

            } else {
                $errorinfo = '没有上传的文件';
                throw new Exception($errorinfo);
            }
        } catch (Exception $e) {


            session(['errorinfo'=>$e->getMessage()]);
            return Redirect::back()->withInput();
            //return view('uploadImage', ['errorinfo' => $e->getMessage()]);
        }

    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        //


        if ($request->has('username')) {
            //$uname=$request->input('username');
            //$upass=$request->input('userpassword');
           /* if($request->hasFile('myfile')) {
                $file = $request->file('myfile');
                $clientName = $file->getClientOriginalName();
                $file->move('file',iconv('utf-8', 'gbk', $clientName));

            }*/


            $input=$request->except('other ');
            $uname=$input['username'];
            $upass=$input['userpassword'];

            return view('testresult',['myusername'=>$uname,'mypassword'=>$upass]);
        }

        return view('test');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function show()
    {
        //
        return view('home');
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
